﻿using System;
using System.Threading.Tasks;
using BookSleeve;
using RedisDemos.Extensions;

namespace RedisDemos.Scenarios
{
    public class LiveDiagnostic : Scenario
    {
        protected override async Task Perform(RedisConnection conn)
        {
            using (var listener = conn.GetOpenSubscriberChannel())
            {
                // PSUBSCRIBE pattern [pattern ...] 
                listener.PatternSubscribe("procs:*", (channel, data) =>
                {
                    "[P] Received {0} on {1}".LogInfo(data.AsString(), channel);
                });

                // SUBSCRIBE channel [channel ...] 
                listener.Subscribe("procs:cpu", (channel, data) =>
                {
                    "[S] Received {0} on {1}".LogInfo(data.AsString(), channel);
                });

                "Ready wait 1 secs".LogInfo();
                await Task.Delay(TimeSpan.FromSeconds(1));

                // PUBLISH channel message 
                await Task.WhenAll(conn.Publish("procs:cpu", "OK"),
                                   conn.Publish("procs:disk", "FAULT"),
                                   conn.Publish("procs:network", "OK"));

                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }
    }
}
